namespace KinonekoSoftware.Studio.Pages.Galleries.Filters
{
    public sealed class CatalogGalleryFilter : ObservableObject, IGalleryFilter
    {
        //-------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------
        private readonly InstanceGalleryViewModel2 _core;
        private readonly IRepository              _repository;
        private readonly InstanceCatalogRoot      _root;

        //-------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------
        private InstanceCatalog _catalog;

        //-------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------
        public CatalogGalleryFilter(IRepository repo, InstanceGalleryViewModel2 core)
        {
            _repository = repo;
            _core       = core ?? throw new ArgumentNullException(nameof(core));
            _root       = repo.GetOrCreate(InstanceCatalogRoot.Create, false);
            Catalogs    = new ObservableCollection<InstanceCatalog>();

            AddCatalogCommand  = new ActionCommand(DoAddCatalogCommand);
            EditCatalogCommand = new FuncCommand<InstanceCatalog>(DoEditCatalogCommand);
            RemoveCatalogCommand = new FuncCommand<InstanceCatalog>(DoRemoveCatalogCommand);
            
        }

        //-------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------
        public void Initialize()
        {
            Catalogs.AddMany(_root.Catalogs, true);
            Catalog = Catalogs.FirstOrDefault();
        }

        //-------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------

        private async void DoAddCatalogCommand()
        {
            var r = await ViewModelBase.SingleText(TitleSR.New, "新建一个设定分类");

            if (!r.IsFinished)
            {
                return;
            }

            var catalog = new InstanceCatalog
            {
                Id            = ID.Get(),
                Name          = r.Value,
                CustomType    = r.Value,
                UseCustomType = true,
                IsVisible     = true,
            };
            
            //
            //
            Catalogs.Add(catalog);
            _root.Catalogs.Add(catalog);
            _repository.Set(_root);
            
            
            //
            //
            Xplat.Debug($"{DateTime.Now} -> 已经更新设定分类");

            //
            //
            Catalog = catalog;
            await ViewModelBase.Success(TitleSR.SuccessOperation, TextSR.Successful_Save);
        }

        private async void DoEditCatalogCommand(InstanceCatalog catalog)
        {
            if (catalog is null)
            {
                return;
            }

            var r = await ViewModelBase.SingleText(TitleSR.Edit, "更新当前设定分类的名字", catalog.Name);

            if (!r.IsFinished)
            {
                return;
            }

            catalog.CustomType = r.Value;
            catalog.Name       = r.Value;
            
            
            //
            //
            Xplat.Debug($"{DateTime.Now} -> 已经更新设定分类");
            
            //
            //
            _repository.Set(_root);
            await ViewModelBase.Success(TitleSR.SuccessOperation, TextSR.Successful_Save);
        }
        
        private async void DoRemoveCatalogCommand(InstanceCatalog catalog)
        {
            if (catalog is null)
            {
                return;
            }

            if (catalog.UseCustomType)
            {
                if (!await ViewModelBase.RemoveItem())
                {
                    return;
                }

                _root.Catalogs.Remove(catalog);
            }
            else
            {
                catalog.IsVisible = false;
            }

            //
            //
            _repository.Set(_root);
            
            //
            //
            Xplat.Debug($"{DateTime.Now} -> 已经更新设定分类");
            
            //
            //
            Catalogs.Remove(catalog);
            await ViewModelBase.Success(TitleSR.SuccessOperation, TextSR.Successful_Save);
        }
        
        //-------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------
        public InstanceCatalog Catalog
        {
            get => _catalog;
            set
            {
                SetValue(ref _catalog, value);

                if (_catalog is not null)
                {
                    _core?.OpenGallery(_catalog);
                }
            }
        }

        public ObservableCollection<InstanceCatalog> Catalogs { get; }

        //-------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------
        public ICommandEX AddCatalogCommand    { get; }
        public ICommandEX EditCatalogCommand   { get; }
        public ICommandEX RemoveCatalogCommand { get; }

        public string Name => "设定类型";
    }
}